<?php
/**
 * Created by PhpStorm.
 * User: 何培春
 * Date: 2017/10/9
 * Time: 11:56
 */

/**
 * @param $count 总数
 * @param $limit 每页数
 * @param $page 检查的页数
 * @return array 返回总页数和页数
 */
function DBPage($count, $limit, $page)
{
    $page = intval($page);
    if ($limit < 1) {
        $limit = 30;
    } else {
        $limit = intval($limit);
    }
    $countPage = ceil($count / $limit);

    if (!is_numeric($page) || $page == 0 || $page == '' || $page == null) {
        return array('countPage' => $countPage, 'page' => 1);
    }
    if ($page > $countPage) {
        return array('countPage' => $countPage, 'page' => $countPage);
    } elseif ($page < 1) {
        return array('countPage' => $countPage, 'page' => 1);
    } else {
        return array('countPage' => $countPage, 'page' => $page);
    }
}


/**
 *
 *
 * desription 判断是否gif动画
 * @param sting $image_file图片路径
 * @return boolean t 是 f 否
 */
function check_gifcartoon($image_file)
{
    $fp = fopen($image_file, 'rb');
    $image_head = fread($fp, 1024);
    fclose($fp);
    return preg_match("/" . chr(0x21) . chr(0xff) . chr(0x0b) . 'NETSCAPE2.0' . "/", $image_head) ? false : true;
}


/**
 * @param string $imgsrc 图片路径
 * @param null $imgdst 处理后的图片保存地址
 * @param null $new_width 新图片宽度,默认和未处理的同样
 * @param null $new_height 新图片的高度,默认和未处理的同样
 * @param int $quality 图片的质量,默认100
 */

function compressed_image($imgsrc = "", $imgdst = null, $new_width = null, $new_height = null, $quality = 100)
{
    list($width, $height, $type) = getimagesize($imgsrc);  //图片信息获取

    if (!$imgdst) {
        $imgdst = $imgsrc;
    }
    if (!$new_height or !$new_width) {
        $new_height = $height;
        $new_width = $width;
    }

    switch ($type) {
        case 1:
            $giftype = check_gifcartoon($imgsrc);
            if ($giftype) {
                //header('Content-Type:image/gif');
                $image_wp = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromgif($imgsrc);
                imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                //75代表的是质量、压缩图片容量大小
                imagejpeg($image_wp, $imgdst, $quality);
                imagedestroy($image_wp);
            }
            break;
        case 2:
            //header('Content-Type:image/jpeg');
            $image_wp = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefromjpeg($imgsrc);
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            //75代表的是质量、压缩图片容量大小
            imagejpeg($image_wp, $imgdst, $quality);
            imagedestroy($image_wp);
            break;
        case 3:
            //header('Content-Type:image/png');
            $image_wp = imagecreatetruecolor($new_width, $new_height);

            $alpha = imagecolorallocatealpha($image_wp, 0, 0, 0, 127);
            imagefill($image_wp, 0, 0, $alpha);

            $image = imagecreatefrompng($imgsrc);//打开图像资源
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagesavealpha($image_wp, true);
            imagepng($image_wp, $imgdst, $quality);
            imagedestroy($image_wp); //销毁图像资源$image_wp=imagecreatetruecolor($new_width, $new_height);

            break;
    }
}

function xss_clean($data)
{
    // Fix &entity\n;
    $data = str_replace(array('&amp;', '&lt;', '&gt;'), array('&amp;amp;', '&amp;lt;', '&amp;gt;'), $data);
    $data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
    $data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
    $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');
    // Remove any attribute starting with "on" or xmlns
    $data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);
    // Remove javascript: and vbscript: protocols
    $data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
    $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
    $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);
    // Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
    $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
    $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
    $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);
    // Remove namespaced elements (we do not need them)
    $data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);
    // http://www.111cn.net/
    do {// Remove really unwanted tags
        $old_data = $data;
        $data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
    } while ($old_data !== $data);
    // we are done...
    return $data;
}


function AdminPasswordEncrypt($password = '', $prefix = '')
{
    return hash('sha256', $prefix . hash('sha256', $password));
}

function UserPasswordEncrypt($password = '', $prefix = '')
{
    return hash('sha256', $prefix . hash('sha256', $password));
}

function GeneratePassword($length = 10)
{
    // 密码字符集，可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
    $password = '';
    for ($i = 0; $i < $length; $i++) {
    // 这里提供两种字符获取方式
    // 第一种是使用 substr 截取$chars中的任意一位字符；
    // 第二种是取字符数组 $chars 的任意元素
    // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
        $password .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    return $password;
}